package org.easy.excel.controller;

import cn.hutool.core.date.StopWatch;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.easy.excel.entity.ExpectDeal;
import org.easy.excel.mapper.ExpectDealMapper;
import org.easy.excel.support.WebExcelExportService;
import org.easy.excel.support.config.entity.ExportStreamSupport;
import org.easy.excel.support.config.impl.SimpleAnnotationExcelConfig;
import org.easy.excel.support.parse.impl.AsyncAnnotationExcelSourceParse;
import org.easy.excel.support.parse.impl.SimpleAnnotationExcelSourceParse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * EasyExcelController
 *
 * @author 李坤
 * @date 2022/11/11 17:24
 */
@RestController
@RequestMapping("/easy/excel")
public class EasyExcelController {

    @Resource
    private ExpectDealMapper expectDealMapper;
    @Resource
    private WebExcelExportService webExcelExportService;
    @Resource
    private AsyncAnnotationExcelSourceParse asyncAnnotationExcelSourceParse;
    @Resource
    private SimpleAnnotationExcelSourceParse simpleAnnotationExcelSourceParse;

    @GetMapping("/export")
    public void exportExpectDeals(HttpServletResponse servletResponse) throws Exception {
        final int constInt = 500;
        final List<ExpectDeal> expectDeals = getExpectDeals();
        final List<ExpectDeal> exportCollection = new ArrayList<>(expectDeals.size() * constInt);

        for (int i = 0; i < constInt; i++) {
            exportCollection.addAll(expectDeals);
        }

        final StopWatch stopWatch = new StopWatch("Excel分析");

        stopWatch.start("Excel导出数据/配置解析");
        final SimpleAnnotationExcelConfig simpleAnnotationExcelConfig = new SimpleAnnotationExcelConfig(simpleAnnotationExcelSourceParse, exportCollection);
        stopWatch.stop();

        stopWatch.start("将数据写出到文件");
        webExcelExportService.export4ServletResponse(servletResponse, simpleAnnotationExcelConfig);
        stopWatch.stop();

        System.out.println(stopWatch);
        System.out.println("Excel导出耗时：" + stopWatch.getTotalTimeSeconds() + " 秒 ");
    }

    @GetMapping("/export0")
    public void exportExpectDeals0(HttpServletResponse servletResponse) throws Exception {
        final StopWatch stopWatch = new StopWatch("Excel分析");
        stopWatch.start("将数据写出到文件");

        final long millis = System.currentTimeMillis();

        final int constInt = 500;
        final List<ExpectDeal> expectDeals = getExpectDeals();
        final List<ExpectDeal> exportCollection = new ArrayList<>(expectDeals.size() * constInt);
        final SimpleAnnotationExcelConfig excelConfig = new SimpleAnnotationExcelConfig(simpleAnnotationExcelSourceParse, expectDeals, false);

        final ExportStreamSupport support4Servlet = webExcelExportService.getExportStreamSupport4Servlet(servletResponse,
                excelConfig.getExcelExportHeaderList(), excelConfig.getWriteHandlerList(), excelConfig.getFileName(),
                excelConfig.getSheetName());

        for (int index = 1; index <= constInt; index++) {
            exportCollection.addAll(expectDeals);

            if (index % 50 == 0) {
                support4Servlet.writer(simpleAnnotationExcelSourceParse.parseExportDataList(exportCollection));
                exportCollection.clear();
            }
        }
        support4Servlet.finish();

        System.out.println(stopWatch);
        System.out.println("Excel导出耗时：" + stopWatch.getTotalTimeSeconds() + " 秒 ");
        System.out.println(System.currentTimeMillis() - millis);
    }

    @GetMapping("/async/export")
    public void exportExpectDeals4Async(HttpServletResponse servletResponse) throws Exception {
        final int constInt = 500;
        final List<ExpectDeal> expectDeals = getExpectDeals();
        final List<ExpectDeal> exportCollection = new ArrayList<>(expectDeals.size() * constInt);

        for (int i = 0; i < constInt; i++) {
            exportCollection.addAll(expectDeals);
        }

        final StopWatch stopWatch = new StopWatch("Excel分析");

        stopWatch.start("Excel导出数据/配置解析");
        final SimpleAnnotationExcelConfig simpleAnnotationExcelConfig = new SimpleAnnotationExcelConfig(asyncAnnotationExcelSourceParse, exportCollection);
        stopWatch.stop();

        stopWatch.start("将数据写出到文件");
        webExcelExportService.export4ServletResponse(servletResponse, simpleAnnotationExcelConfig);
        stopWatch.stop();

        System.out.println(stopWatch);
        System.out.println("Excel导出耗时：" + stopWatch.getTotalTimeSeconds() + " 秒 ");
    }

    @GetMapping("/parse")
    public String exportExpectDeals4Parse() throws Exception {
        final int constInt = 1000;
        final List<ExpectDeal> expectDeals = getExpectDeals();
        final List<ExpectDeal> exportCollection = new ArrayList<>(expectDeals.size() * constInt);

        for (int i = 0; i < constInt; i++) {
            exportCollection.addAll(expectDeals);
        }

        final StopWatch stopWatch = new StopWatch("Excel分析");

        stopWatch.start("Excel导出数据/配置解析");
        new SimpleAnnotationExcelConfig(simpleAnnotationExcelSourceParse, exportCollection);
        stopWatch.stop();

        return "Excel解析耗时：" + stopWatch.getTotalTimeSeconds() + " 秒 ";
    }

    @GetMapping("/async/parse")
    public String exportExpectDeals4AsyncParse() throws Exception {
        final int constInt = 1000;
        final List<ExpectDeal> expectDeals = getExpectDeals();
        final List<ExpectDeal> exportCollection = new ArrayList<>(expectDeals.size() * constInt);

        for (int i = 0; i < constInt; i++) {
            exportCollection.addAll(expectDeals);
        }

        final StopWatch stopWatch = new StopWatch("Excel分析");

        stopWatch.start("Excel导出数据/配置解析");
        new SimpleAnnotationExcelConfig(asyncAnnotationExcelSourceParse, exportCollection);
        stopWatch.stop();

        return "Excel解析耗时：" + stopWatch.getTotalTimeSeconds() + " 秒 ";
    }

    /*----------------------------------------------private methods-----------------------------------------------*/


    /**
     * 获取要导出的 预期收益型产品数据
     *
     * @return 预期收益型产品数据
     */
    private List<ExpectDeal> getExpectDeals() {
        final QueryWrapper<ExpectDeal> wrapper = new QueryWrapper<>();
        wrapper.eq("TM_EFFECTFLAG", "E");

        return expectDealMapper.selectList(wrapper);
    }

}
